﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BW.Mes.WinForms.Production.ProductionDate
{
    public partial class AddProdectionDateForm : DevExpress.XtraEditors.XtraForm
    {
        // 定义属性用于获取和设置日期
        public DateTime SelectedDate
        {
            get { return dateEdit.DateTime; }
            set { dateEdit.DateTime = value; }
        }

        // 定义属性用于获取和设置类型（0=休，1=班）
        public int DateType
        {
            get { return radioWorkDay.Checked ? 1 : 0; }
            set
            {
                if (value == 1)
                {
                    radioWorkDay.Checked = true;
                    radioRestDay.Checked = false;
                }
                else
                {
                    radioRestDay.Checked = true;
                    radioWorkDay.Checked = false;
                }
            }
        }

        public AddProdectionDateForm()
        {
            InitializeComponent();

            // 注册事件
            this.Load += AddProdectionDateForm_Load;
            this.btnOK.Click += BtnOK_Click;
        }

        private void AddProdectionDateForm_Load(object sender, EventArgs e)
        {
            // 如果未设置日期，则默认为当天
            if (dateEdit.EditValue == null)
            {
                dateEdit.DateTime = DateTime.Today;
            }

            // 如果未设置类型，则默认为班（1）
            if (!radioRestDay.Checked && !radioWorkDay.Checked)
            {
                radioWorkDay.Checked = true;
            }
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            // 验证输入
            if (dateEdit.EditValue == null)
            {
                XtraMessageBox.Show("请选择日期", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                dateEdit.Focus();
                this.DialogResult = DialogResult.None;
                return;
            }

            if (!radioRestDay.Checked && !radioWorkDay.Checked)
            {
                XtraMessageBox.Show("请选择日期类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.DialogResult = DialogResult.None;
                return;
            }

            // 验证通过，设置对话框结果为OK
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// 静态方法，显示对话框并返回结果
        /// </summary>
        /// <param name="date">初始日期</param>
        /// <param name="type">初始类型（0=休，1=班）</param>
        /// <param name="selectedDate">选择的日期</param>
        /// <param name="selectedType">选择的类型</param>
        /// <returns>是否确认</returns>
        public static bool ShowDialog(DateTime date, int type, out DateTime selectedDate, out int selectedType)
        {
            using (AddProdectionDateForm form = new AddProdectionDateForm())
            {
                // 设置初始值
                form.SelectedDate = date;
                form.DateType = type;

                // 显示对话框
                DialogResult result = form.ShowDialog();

                // 获取结果
                selectedDate = form.SelectedDate;
                selectedType = form.DateType;

                return result == DialogResult.OK;
            }
        }
    }
}